import { defineStore, storeToRefs } from 'pinia';
import { getUuid } from '~/utils';

const CLIENT_ID_KEY = 'CLIENT_ID_KEY';

export const ASIDE_MENU_KEY = 'ASIDE_MDENU_KEY';

const DEFAULT_ASIDE_MENU = 'project';

export const useSettingStore = defineStore('setting', {
  state: () => ({
    _id: null as string | null,
    asideMenu: DEFAULT_ASIDE_MENU
  }),
  getters: {
    clientId: ({ _id }) => {
      if (_id) {
        return _id;
      } else {
        const item = localStorage.getItem(CLIENT_ID_KEY);
        if (item) {
          _id = item;
        } else {
          let uuid = getUuid();
          _id = uuid;
          localStorage.setItem(CLIENT_ID_KEY, uuid);
        }
        // 响应式
        return _id;
      }
    }
  },
  actions: {
    resetAsideMenu() {
      localStorage.removeItem(ASIDE_MENU_KEY);
      this.asideMenu = DEFAULT_ASIDE_MENU;
    }
  }
});

export function getSettingStore() {
  return storeToRefs(useSettingStore());
}
